<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!-- 数据库基础操作 -->
<mapper namespace = "${basePackage}.mapper.${module}.${currModule}.${entityName}Mapper">
	<!-- 查询语句中form之前的语句 -->
	<sql id="selectList">
		select 
		<if test="sql==null or sql.baseSelect==null or sql.baseSelect">
			<#list fieldList as field>
			<#if (field_index>0)>,</#if>t.${field.columnName} ${field.columnAlias}
			</#list>
		</if>
		<if test="sql!=null and sql.selectList!=null">
			<foreach collection="sql.selectList" item="item" open="," separator=",">
				${valueBegin2}item.field${valueEnd}<if test="item.alias!=null"> ${valueBegin2}item.alias${valueEnd}</if>
			</foreach>
		</if>
	</sql>
	
	<!-- 查询语句中form之前的语句-查询数量 -->
	<sql id="selectCount">
		select 
		<choose>
			<when test="sql==null or sql.baseSelect==null or sql.baseSelect">count(1)</when>
			<otherwise>
				<foreach collection="sql.selectList" item="item" separator=",">
					${valueBegin2}item.field${valueEnd}<if test="item.alias!=null"> ${valueBegin2}item.alias${valueEnd}</if>
				</foreach>
			</otherwise>
		</choose>
	</sql>
	
	<!-- 查询语句中form及之后where之前的语句 -->
	<sql id="fromList">
		<if test="sql==null or sql.baseFrom==null or sql.baseFrom">
			from ${table} t
		</if>
		<if test="sql!=null and sql.fromList!=null">
			<foreach collection="sql.fromList" item="item">
				<if test="item.joinOnType!=null">${valueBegin2}item.joinOnType${valueEnd} </if>${valueBegin2}item.tableName${valueEnd}<if test="item.alias!=null"> ${valueBegin2}item.alias${valueEnd} </if><if test="item.joinOnType!=null">on </if><if test="item.joinCondition!=null">${valueBegin2}item.joinCondition${valueEnd}</if>
			</foreach>
		</if>
	</sql>
	
	<#if primaryKeyList??>
	<!-- 根据主键查询单条数据 -->
	<select id="findSingle" resultType="${basePackage}.pojo.dao.${module}.${currModule}.${entityName}DataDo">
		<include refid="selectList"/>
		<include refid="fromList"/>
		<where>
			<choose>
				<when test="sql!=null and sql.whereList!=null">
					<include refid="sqlWhere"/>
				</when>
				<otherwise>
					<#if isDelete><if test="isDelete!=null">and t.is_delete_=${valueBegin}isDelete${valueEnd}</if></#if>
					<#list primaryKeyList as field>and t.${field.columnName}=${valueBegin}id${valueEnd}</#list>
				</otherwise>
			</choose>
		</where>
		<include refid="baseGroupBy"/>
		<include refid="baseOrderBy"/>
		<include refid="baseLimit"/>
	</select>
	</#if>
	
	<!-- 根据条件查询数量 -->
	<select id="findCount" resultType="int">
		select count(1)
		<include refid="fromList"/>
		<include refid="baseWhere"/>
		<include refid="baseGroupBy"/>
	</select>
	
	<!-- 根据条件查询数据-返回列表 -->
	<select id="findList" resultType="${basePackage}.pojo.dao.${module}.${currModule}.${entityName}DataDo">
		<include refid="selectList"/>
		<include refid="fromList"/>
		<include refid="baseWhere"/>
		<include refid="baseGroupBy"/>
		<include refid="baseOrderBy"/>
		<include refid="baseLimit"/>
	</select>
	
	<!-- 根据条件查询数据-返回游标 -->
	<select id="findBigData" resultType="${basePackage}.pojo.dao.${module}.${currModule}.${entityName}DataDo" resultSetType="FORWARD_ONLY" fetchSize="1000">
		<include refid="selectList"/>
		<include refid="fromList"/>
		<include refid="baseWhere"/>
		<include refid="baseGroupBy"/>
		<include refid="baseOrderBy"/>
	</select>
	
	<!-- 查询语句where及之后的语句 -->
	<sql id="baseWhere">
		<where>
			<if test="sql==null or sql.baseWhere==null or sql.baseWhere">
				<#if isDelete><if test="isDelete!=null">and t.is_delete_=${valueBegin}isDelete${valueEnd}</if></#if>
				<#list fieldList as field>
				<#if isDelete && field.fieldSampleName=='isDelete'><#else>
				<if test='${field.fieldSampleName} != null<#if field.fieldType=="String"> and ${field.fieldSampleName}!=""</#if>'>and t.${field.columnName} = ${valueBegin}${field.fieldSampleName}${valueEnd}</if>
				<#if field.primaryKey>
				<if test='${field.fieldSampleName}List != null and ${field.fieldSampleName}List.size()>0'>
					and t.${field.columnName} in
					<foreach collection="${field.fieldSampleName}List" item="${field.fieldSampleName}" open="(" close=")" separator=",">
						${valueBegin}${field.fieldSampleName}${valueEnd}
					</foreach>
				</if>
				</#if>
				</#if>
				</#list>
			</if>
			<include refid="sqlWhere"/>
		</where>
	</sql>
	
	<!-- 自定义查询条件的语句 -->
	<sql id="sqlWhere">
		<if test='sql!=null and sql.whereList!=null'>
			<foreach collection="sql.whereList" item="where">
				${valueBegin2}where.logic${valueEnd} <if test="where.start!=null and where.start">(</if>
				<choose>
					<when test='where.compare.contains("EXISTS")'>
						${valueBegin2}where.compare${valueEnd}(${valueBegin2}where.valueList[0]${valueEnd})
					</when>
					<otherwise>
						${valueBegin2}where.field${valueEnd} ${valueBegin2}where.compare${valueEnd}
						<choose>
							<when test='where.compare=="IN"'>
								<foreach collection="where.valueList" item="value" open="(" close=")" separator=",">
									${valueBegin2}value${valueEnd}
								</foreach>
							</when>
							<when test='where.compare=="BETWEEN"'>
								${valueBegin2}where.valueList[0]${valueEnd} AND ${valueBegin2}where.valueList[1]${valueEnd}
							</when>
							<otherwise>
								${valueBegin2}where.valueList[0]${valueEnd}
							</otherwise>
						</choose>
					</otherwise>
				</choose>
				<if test="where.end!=null and where.end">)</if>
			</foreach>
		</if>
	</sql>
	
	<!-- 自定义分组语句 -->
	<sql id="baseGroupBy">
		<if test="sql!=null and sql.groupByList!=null">
			group by
			<foreach collection="sql.groupByList" item="groupBy" separator=",">
				${valueBegin2}groupBy${valueEnd}
			</foreach>
		</if>
	</sql>
	
	<!-- 自定义排序语句 -->
	<sql id="baseOrderBy">
		<if test="sql!=null and sql.orderByList!=null">
			order by
			<foreach collection="sql.orderByList" item="orderBy" separator=",">
				${valueBegin2}orderBy.field${valueEnd} ${valueBegin2}orderBy.ascDesc${valueEnd}
			</foreach>
		</if>
	</sql>
	
	<!-- 自定义Limit语句 -->
	<sql id="baseLimit">
		<if test="sql!=null and sql.firstSize!=null">limit ${valueBegin}sql.firstSize${valueEnd},${valueBegin}sql.pageSize${valueEnd}</if>
	</sql>
	
	<!-- 单条数据插入-如果主键为自增，则插入之后设置主键的值 -->
	<insert id="insert"<#if autoIncrement??> useGeneratedKeys="true" keyProperty="${autoIncrement.fieldSampleName}" keyColumn="${autoIncrement.columnName}"</#if>>
		<#assign length=fieldList?size/>insert into ${table}
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<#list fieldList as field>
			${field.columnName},
			</#list>
		</trim>
		values
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<#list fieldList as field>
				<#if stringUtils.contains("createTime,changeTime",field.fieldSampleName,',')>
			SYSDATE(),
				<#else>
			${valueBegin}${field.fieldSampleName}${valueEnd},
				</#if>
			</#list>
		</trim>
	</insert>
	
	<!-- 多条数据插入-如果主键为自增，则插入之后设置主键的值 -->
	<insert id="insertBatch"<#if autoIncrement??> useGeneratedKeys="true" keyProperty="list.${autoIncrement.fieldSampleName}" keyColumn="${autoIncrement.columnName}"</#if>>
		<#assign length=fieldList?size/>insert into ${table}
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<#list fieldList as field>
			${field.columnName},
			</#list>
		</trim>
		values
		<foreach collection="list" item="item" separator=",">
			<trim prefix="(" suffix=")" suffixOverrides=",">
				<#list fieldList as field>
					<#if stringUtils.contains("createTime,changeTime",field.fieldSampleName,',')>
				SYSDATE(),
					<#else>
				${valueBegin}item.${field.fieldSampleName}${valueEnd},
					</#if>
				</#list>
			</trim>
		</foreach>
	</insert>
	
	<#if primaryKeyList??>
	<!-- 单条数据更新（为空不更新） -->
	<update id="update">
		update ${table}
		<set>
			<#list fieldList as field>
				<#list primaryKeyList as pkField>
					<#if field.columnName==pkField.columnName>
						<#assign pk=true/>
					<#else>
						<#assign pk=false/>
					</#if>
				</#list>
				<#if !pk && !stringUtils.contains("isDelete",field.fieldSampleName,',')>
					<#if !stringUtils.startsWith(field.fieldSampleName,'create')>
						<#if stringUtils.contains("changeTime",field.fieldSampleName,',')>
			${field.columnName} = SYSDATE(),
						<#else>
			<if test="${field.fieldSampleName} != null">${field.columnName} = ${valueBegin}${field.fieldSampleName}${valueEnd},</if>
						</#if>
					</#if>
				</#if>
			</#list>
		</set>
		<where>
			<choose>
				<when test="id != null"><#list primaryKeyList as field>and ${field.columnName}=${valueBegin}${field.fieldSampleName}${valueEnd}</#list></when>
				<otherwise><include refid="sqlWhere"/></otherwise>
			</choose>
		</where>
	</update>
	
	<!-- 单条数据更新（更新所有业务相关字段） -->
	<update id="updateAuto">
		update ${table}
		<set>
			<if test='sql!=null and sql.setList!=null and sql.setList?size() &gt; 0'>
				<foreach collection="sql.setList" item="item" separator=",">
					${valueBegin2}item.field${valueEnd} = ${valueBegin2}item.value${valueEnd}
				</foreach>
			</if>
			<if test="changeUser != null">change_user_ = ${valueBegin}changeUser${valueEnd},</if>
			change_time_ = SYSDATE(),
		</set>
		<where>
			<choose>
				<when test="id != null"><#list primaryKeyList as field>and ${field.columnName}=${valueBegin}${field.fieldSampleName}${valueEnd}</#list></when>
				<otherwise><include refid="sqlWhere"/></otherwise>
			</choose>
		</where>
	</update>
	
	<!-- 多条数据更新（为空不更新） -->
	<update id="updateBatch">
		<foreach collection="list" item="item" separator=";">
			update ${table}
			<set>
				<#list fieldList as field>
					<#list primaryKeyList as pkField>
						<#if field.columnName==pkField.columnName>
							<#assign pk=true/>
						<#else>
							<#assign pk=false/>
						</#if>
					</#list>
					<#if !pk && !stringUtils.contains("isDelete",field.fieldSampleName,',')>
						<#if !stringUtils.startsWith(field.fieldSampleName,'create')>
							<#if stringUtils.contains("changeTime",field.fieldSampleName,',')>
				${field.columnName} = SYSDATE(),
							<#else>
				<if test="item.${field.fieldSampleName} != null">${field.columnName} = ${valueBegin}item.${field.fieldSampleName}${valueEnd},</if>
							</#if>
						</#if>
					</#if>
				</#list>
			</set>
			<where>
				<#list primaryKeyList as field>
				and ${field.columnName}=${valueBegin}item.${field.fieldSampleName}${valueEnd}
				</#list>
			</where>
		</foreach>
	</update>
	</#if>
	
	<#if isDeleteField??>
	<!-- 根据主键删除数据(逻辑删除) -->
	<update id="delete">
		update ${table} 
		set ${isDeleteField.columnName}=1,change_time_=SYSDATE()
		<where>
			<if test='sql!=null'>
				<include refid="sqlWhere"/>
			</if>
			<if test='sql==null'>
				<#list primaryKeyList as field>and ${field.columnName}=${valueBegin}${field.fieldSampleName}${valueEnd}</#list>
			</if>
		</where>
	</update>
	
	<!-- 根据主键集合批量删除(逻辑删除) -->
	<update id="deleteBatch">
		update ${table} 
		set ${isDeleteField.columnName}=1,change_time_=SYSDATE()
		<where>
			<#list primaryKeyList as field>and ${field.columnName} in
			<foreach collection="idList" item="id" open="(" close=")" separator=",">
				${valueBegin}id${valueEnd}
			</foreach>
			</#list>
		</where> 
	</update>
	
	<!-- 根据主键删除数据(物理删除) -->
	<delete id="deleteReal">
		delete from ${table} 
		<where>
			<if test='sql!=null'>
				<include refid="sqlWhere"/>
			</if>
			<if test='sql==null'>
				<#list primaryKeyList as field>and ${field.columnName}=${valueBegin}${field.fieldSampleName}${valueEnd}</#list>
			</if>
		</where>
	</delete>
	
	<!-- 根据主键集合批量删除数据(物理删除) -->
	<delete id="deleteRealBatch">
		delete from ${table}
		<where>
			<#list primaryKeyList as field>and ${field.columnName} in
			<foreach collection="idList" item="id" open="(" close=")" separator=",">
				${valueBegin}id${valueEnd}
			</foreach>
			</#list>
		</where> 
	</delete>
	
	<#else>
	<!-- 根据主键删除数据(物理删除) -->
	<delete id="delete">
		delete from ${table}
		<where>
			<if test='sql!=null'>
				<include refid="sqlWhere"/>
			</if>
			<if test='sql==null'>
				<#list primaryKeyList as field>and ${field.columnName}=${valueBegin}${field.fieldSampleName}${valueEnd}</#list>
			</if>
		</where>
	</delete>
	
	<!-- 根据主键集合批量删除数据(物理删除) -->
	<delete id="deleteBatch">
		delete from ${table}
		<where>
			<#list primaryKeyList as field>and ${field.columnName} in
			<foreach collection="idList" item="id" open="(" close=")" separator=",">
				${valueBegin}id${valueEnd}
			</foreach>
			</#list>
		</where> 
	</delete>
	
	</#if>
</mapper>